package com.cskaoyan.mall.pay.service.impl;

import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.pay.constants.PayResult;
import com.cskaoyan.mall.pay.constants.PayRetCode;
import com.cskaoyan.mall.pay.converter.PaymentConverter;
import com.cskaoyan.mall.pay.dal.entitys.Payment;
import com.cskaoyan.mall.pay.dal.persistence.PaymentMapper;
import com.cskaoyan.mall.pay.form.PaymentRequest;
import com.cskaoyan.mall.pay.remote.OrderApiClient;
import com.cskaoyan.mall.pay.service.PayCoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

/**
 * @auther cskaoyan
 * @date 2022/8/27:11:20
 */
@Service
public class PayCoreServiceImpl implements PayCoreService {

    @Autowired
    PayHelper payHelper;
    @Autowired
    PaymentMapper paymentMapper;
    @Autowired
    PaymentConverter paymentConverter;
    @Autowired
    OrderApiClient orderApiClient;

    @Override
    public String aliPay(PaymentRequest request) {
       // 1. 请求支付宝支付，获取支付二维码
        String qrCode = payHelper.test_trade_precreate(request);

        if (qrCode == null) {
            // 获取支付二维码
            throw new BizException(PayRetCode.GET_CODE_FALIED.getCode(),
                    PayRetCode.GET_CODE_FALIED .getMsg());
        }

        // 向tb_payment插入该订单对应的一条支付记录(状态为 TRADE_PROCESSING)
        Payment payment = paymentConverter.paymentRequest2Payment(request);
        payment.setStatus(PayResult.TRADE_PROCESSING.getCode());
        payment.setPayerAmount(request.getMoney());
        payment.setCreateTime(new Date());
        payment.setUpdateTime(new Date());

        paymentMapper.insertSelective(payment);
        return qrCode;
    }

    @Override
    public boolean queryAlipayRet(String orderId) {

        // 访问支付宝支付，查询结果
        boolean success = payHelper.test_trade_query(orderId);

        if (success) {
            // 如果支付成功
            // 1. 修改tb_payment中订单的支付结果TRADE_SUCCESS， 支付成功时间，支付完成时间
            Payment payment = new Payment();
            payment.setStatus(PayResult.TRADE_SUCCESS.getCode());
            payment.setPaySuccessTime(new Date());
            payment.setCompleteTime(new Date());
            payment.setUpdateTime(new Date());
            Example example = new Example(Payment.class);
            example.createCriteria().andEqualTo("orderId",orderId);
            paymentMapper.updateByExampleSelective(payment, example);
            //  通过服务调用来实现 调用订单服务
            // 2. 修改订单状态
            // 3. 根据订单id，查询出订单中的所有订单商品条目，根据每一个订单商品条目购买的数量，扣减
            //    对应商品的锁定库存
            orderApiClient.payOrderSuccess(orderId);
            return true;

        } else {
            // 如果支付失败
            // 修改tb_payment中订单的支付结果TRADE_FAILURE， 支付完成时间
            Payment payment = new Payment();
            payment.setStatus(PayResult.TRADE_FAILURE.getCode());
            payment.setCompleteTime(new Date());
            payment.setUpdateTime(new Date());
            Example example = new Example(Payment.class);
            example.createCriteria().andEqualTo("orderId",orderId);
            paymentMapper.updateByExampleSelective(payment, example);
        }

        return false;
    }
}
